﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

public partial class admin_system_enumitemeditor : PageBase
{
    public string PageTitle = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin("system configuration");

        ///保存
        if (RequestBase.GetFormString("__EVENTTARGET") == "save")
        {
            SaveInfo();
        }

        if (!IsPostBack)
        {
            if (RequestBase.GetQueryString("otype") == "modify")
            {
                PageTitle = "Modify";
                Initialize();
            }
            else
            {
                PageTitle = "Add";

                if (Utils.IsGuid(RequestBase.GetQueryString("parentid")))
                {
                    Model.Sys.EnumDef _obj = BLL.Sys.EnumDef.GetObjById(RequestBase.GetQueryString("parentid"));
                    txtEnumKey.Value = _obj.EnumKey;
                }
                else
                {
                    txtEnumKey.Value = RequestBase.GetQueryString("enumkey");
                }
            }
        }
    }

    #region 信息初始化
    void Initialize()
    {
        GetObj = BLL.Sys.EnumItem.GetObjById(RequestBase.GetQueryString("enumitemid"));
    }

    ///// <summary>
    ///// 信息实体
    ///// </summary>
    Model.Sys.EnumItem GetObj
    {
        get
        {
            Model.Sys.EnumItem _cat = new Model.Sys.EnumItem();
            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("enumitemid").Trim()))
            {
                _cat.EnumItemId = RequestBase.GetQueryString("enumitemid");
            }
            
            _cat.EnumKey = txtEnumKey.Value.Trim();
            _cat.EnumText = txtEnumText.Value.Trim();
            _cat.EnumEnText = txtEnumEnText.Value.Trim();
            _cat.EnumValue = txtEnumValue.Value.Trim();
            _cat.SortIndex = decimal.Parse(txtSortIndex.Value.Trim()); 

            return _cat;
        }
        set
        {
            if (value != null)
            {
                txtEnumKey.Value = value.EnumKey.Trim();
                txtEnumText.Value = value.EnumText.Trim();
                txtEnumEnText.Value = value.EnumEnText.Trim();
                txtEnumValue.Value = value.EnumValue.Trim();
                txtSortIndex.Value = value.SortIndex.ToString();
            }
        }
    }
    #endregion

    void SaveInfo()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {

            res = BLL.Sys.EnumItem.Add(GetObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.Sys.EnumItem.Update(GetObj);
        }

        switch (res)
        {
            case "succeeded":
                if (RequestBase.GetQueryString("otype") == "add")
                {
                    if (RequestBase.GetFormString("__EVENTARGUMENT") == "saveandclose")
                        JScript.AlertAndReturnValueToParent("Information saved successfully!", "window.opener.window.location.reload(); window.close();");
                    else
                        JScript.AlertAndRedirect("Information saved successfully!", "../system/enumitemeditor.aspx?otype=add&enumkey=" + txtEnumKey.Value.Trim());
                }
                else
                {
                    if (RequestBase.GetFormString("__EVENTARGUMENT") == "saveandclose")
                        JScript.AlertAndReturnValueToParent("Information saved successfully!", "window.opener.window.location.reload(); window.close();");
                    else
                        JScript.AlertAndRedirect("Information saved successfully!", "../system/enumitemeditor.aspx?otype=add&enumkey=" + txtEnumKey.Value.Trim());
                }
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
}
